package actions.visitors;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import veilingDomein.Blocked;
import veilingDomein.User;
import veilingService.IUserService;
import veilingService.ServiceProvider;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class Login extends ActionSupport implements SessionAware {

	private User user;
	@SuppressWarnings("rawtypes")
	private Map session;
	private String username;
	private String password;
	private IUserService ius = ServiceProvider.getUserService();

	@SuppressWarnings("unchecked")
	public String execute() {
		session.put("user", user);
		System.out.println("Rechten id: " + user.getRechten());
		if(user.getRechten() == 0){
			return "manager";
		}
		return SUCCESS;
	}
// test
	public void validate() {
		password = password.trim();
		username = username.trim();

		if (username.length() == 0) {
			addFieldError("username", "naam is verplicht");
		}
		if (password.length() == 0) {
			addFieldError("password", "wachtwoord is verplicht");
		} else {

			user = ius.getUser(username);
			if (user != null) {
				if ((user == null) || !(user.getPassword().equals(password))) {
					addFieldError("username", "naam of wachtwoord is niet juist");
				}
				Blocked block = ius.isBlocked(user.getUsername());
				if (block != null) {
					addFieldError("username", "Dit account is geblokkeerd tot: " + block.getEind());
				}
			} else {
				addFieldError("username", "naam of wachtwoord is niet juist");
			}
		}
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@SuppressWarnings("rawtypes")
	public void setSession(Map session) {
		this.session = session;
	}

	public User getUser() {
		return user;
	}

}
